package com.example;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 站点
 */
public class Site {

    /**
     * 站点名称
     */
    private String name;

    private Site nextSite;

    private int nextArrivalTime;

    /**
     * 当前站点的乘客
     */
    private List<Guest> guests = new LinkedList<>();

//    private Site preSite;

//    private int preArrivalTime;

    public Site(String name) {
        this.name = name;
    }

    public void setNextSite(Site nextSite, int nextArrivalTime) {
        this.nextSite = nextSite;
        this.nextArrivalTime = nextArrivalTime;

    }

    public Site getNextSite() {
        return nextSite;
    }

    public int getNextArrivalTime() {
        return nextArrivalTime;
    }

    public String getName() {
        return name;
    }

    public synchronized void addGuest(Guest guest) {
        guests.add(guest);
    }

    /**
     * 从站点获取可以上车的乘客
     *
     * @param allowUpGuest
     * @return
     */
    public synchronized List<Guest> getUpGuests(int allowUpGuest) {
        List<Guest> upGuests = new LinkedList<>();
        if (allowUpGuest > guests.size()) {
            upGuests = guests;
            guests = new LinkedList<>();
        } else {
            for (int i = 0; i < allowUpGuest; i++) {
                upGuests.add(guests.remove(0));
            }
        }
        return upGuests;
    }

    @Override
    public String toString() {
        return "Site{" +
                "站名: '" + name + '\'' +
                //防止循环依赖调用
                ", 下一站: '" + nextSite.getName() + "'" +
                ", 等候乘客数: " + guests.size() +
                '}';
    }
}
